import { useMemo } from "react";
import { useSelector } from "react-redux";
import { RootState } from "./index";

export function useCar() {
  const car = useSelector((state: RootState) => state.car.car);

  const allPrice = useMemo(() => {
    return car.reduce((p, n) => {
      return n.isChecked ? p + n.price * n.count : p;
    }, 0);
  }, [car]);

  const allCount = useMemo(() => {
    return car.reduce((p, n) => {
      return n.isChecked ? p + n.count : p;
    }, 0);
  }, [car]);

  const count = useMemo(() => {
    return car.reduce((p, n) => {
      return p + n.count;
    }, 0);
  }, [car]);

  const allCheck = useMemo(() => {
    return car.every(v => v.isChecked)
  }, [car])

  return { allPrice, allCount, count, allCheck };
}
